<template>
	<view :style="[AppSkinVar]">
		<e-loading-page :loading="loading||payLoading"  >
			<template #icon>
				<loader ></loader>
			</template>
		</e-loading-page>
		<yuncangGoodsCard :goods="info" :goodsKeys="{
			image: 'coverUrl',
			price: 'price',
			name: 'title',
			qty: 'commodityCount',
		}"
		:stock="qty"
		></yuncangGoodsCard>
		<pay-popup :show="show" @close="show = false" @pay="pay"></pay-popup>

		<view style="height: 140rpx;"></view>
		<view class="fixed-button u-flex u-flex-between bg-white">
			<view class="u-flex u-flex-between">
				<text class="u-font-30">{{ $t('cart.总计') }}：</text>
				<text class="c-red u-font-36">{{$unitEn}}{{ info.payMoney }}</text>
			</view>
			<button class="bg-primary c-white" @tap="$u.throttle(buy, 1000)">{{ $t('classify.去支付') }}</button>
		</view>
	</view>
</template>

<script>
import { postYuncangPreviewOneGoodsOrder, postYuncangPurchaseOneGoods,getPayResult } from 'api/yuncang.js'
// 支付mixin
import payMixIn from 'common/mixin/payMixIn.js'
import { mapActions } from 'vuex'
import yuncangGoodsCard from './child/yuncang-goods-card.vue'
export default {
	mixins: [payMixIn],
	components:{
		yuncangGoodsCard
	},
	data() {
		return {
			params: {},
			info: {},
			show: false,
			loading: false,
			qty:1
		}
	},

	onLoad(opt) {
		this.params = opt
		this.loading = true
		this.getData()
		// #ifdef HTML5 || PREVIEW
		this.getH5PayResult('yuncang/order/success','yuncangPayOrderId',getPayResult)
		// #endif
		this.qty = opt.qty || 1
	},
	methods: {
		getData() {
			let params = { ...this.params}
			delete params.type
			postPreviewOneGoodsOrder(params).then(res => {
				this.loading = false
				if (res.code == 200) {
					this.info = res.repertory
					this.info.payMoney = res.payMoney
				}
			})
		},
		buy() {
			// if (!this.address.id) {
			// 	this.$toastApp(this.$t('classify.请先添加收货地址再支付'))
			// 	return
			// }
			this.show = true
		},
		// 支付
		pay(payId, password = null, isDefault = 'Y',payType) {
			let params = { ...this.params }
			delete params.type
			let data = {
				payId,
				...params,
				fromType: getApp().globalData.payFromType
			}
			password && (data['payword'] = password)
			this.loading = true
			this.requestPay({
				api:postPurchaseOneGoods,
				params:[data],
				isDefault,
				successUrl: id => `yuncang/order/success?type=4&id=${id}`,
				errorUrl:msg => `yuncang/order/error?msg=${msg}`,
				storageKey:'yuncangPayOrderId',
				orderUrl:'yuncang/order/index',
				payType,
				payResultApi:getPayResult
			})
		}
	}
}
</script>
<style>
page {
	padding-top: 20rpx;
}
</style>
<style lang="scss" scoped>

.fixed-button {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
	height: 112rpx;
	padding: 0 28rpx;
	button {
		width: 240rpx;
		height: 80rpx;
		line-height: 80rpx;
		border-radius: 40rpx;
		margin: 0;
	}
}
</style>
